package com.homwee.factory.pageData;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Handler;

import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


import com.homwee.factory.FactoryKeypadTestActivity;
import com.homwee.factory.TvToolService;
import com.homwee.factory.FactoryActivity;
import com.homwee.factory.FactoryModeService;
import com.homwee.factory.R;
import com.homwee.factory.util.Contants;
import com.homwee.listMenu.util.MenuItemData;
import com.homwee.tvap.common.srvfunc.FactorySetting;

import com.mediatek.twoworlds.factory.MtkTvFApiInformation;
import com.mediatek.twoworlds.factory.MtkTvFApiSystem;


public class PageData1 extends PageData{
	
	private String pqStr =null;
	private String aqStr =null;
	private String fpStr =null;
	private String DataVersionStr =null;
	public String selProjectid =null;
	List<MenuItemData> mSubList =new ArrayList<MenuItemData>();
	
	private Context context;
    Handler mHandler =new Handler();
    Runnable mRunnable =new Runnable() {
		
		@Override
		public void run() {
			// TODO Auto-generated method stub
			factorySetting.setProductIndex(productModel.mInitValue);
			productModel.updateValue(factorySetting.getCurProductName()/*+"系列"*/);
			panelSelect.mMaxValue =factorySetting.getPanelNum()-1;
			panelSelect.mInitValue =0;
		}
	};
	
	public PageData1(Context context,FactoryActivity mFactoryActivity){
		super(mFactoryActivity);
		this.context = context;
//		mcomSettingManager=comSettingManager.getInstance(null);
	}
	
	MenuItemData productModel;
	public MenuItemData panelSelect;
	
	MenuItemData projectSelect;
	public MenuItemData projectID;
	MenuItemData projectSaveToEep;
	MenuItemData projectSave;
//	MenuItemData projectPanel;
//	MenuItemData projectCountry;
//	MenuItemData projectLogo;
//	MenuItemData projectMusic;
//	MenuItemData projectAnimation;
//	MenuItemData projectKeymap;
//	MenuItemData projectInputSource;
	public List<MenuItemData> LoadPageData(){
		mListItemData.clear();
		mSubList.clear();
        
		pqStr= factorySetting.getPQVersion();
		MenuItemData factoryPqNameItem  = new MenuItemData(pqStr, mFactoryActivity.getResources()
				.getString(R.string.factory_pq_version),pqStr,
				MenuItemData.EN_ItemType.EN_OPTIONTEXTVIEW);
		
		aqStr=factorySetting.getAQVersion();
		MenuItemData factoryAqNameItem  = new MenuItemData(aqStr, mFactoryActivity.getResources()
				.getString(R.string.factory_aq_version),aqStr,
				MenuItemData.EN_ItemType.EN_OPTIONTEXTVIEW);
		
		fpStr=factorySetting.getFPVersion();
		MenuItemData factoryFpNameItem  = new MenuItemData(fpStr, mFactoryActivity.getResources()
				.getString(R.string.factory_fp_version),fpStr,
				MenuItemData.EN_ItemType.EN_OPTIONTEXTVIEW);
		
		String panelSelectStr = mFactoryActivity.getResources().getString(R.string.factory_screen_parameter_choice);

		panelSelect =new MenuItemData(0,panelSelectStr,0,0,0,"",1);
		panelSelect.updateValue(factorySetting.getPanelName());

		panelSelect.setOnEnterKey(new MenuItemData.onEnterRightKey() {
			
			@Override
			public void onEnterRightKey() {
				// TODO Auto-generated method stub
				
				Intent selectIntent = new Intent();
				selectIntent.setComponent(new ComponentName(
						"com.homwee.factory",
						"FactorySelectActivity"));
				mFactoryActivity.startActivityForResult(selectIntent,100);
			}
		});
		
		/*2019-2-13 TvosStr =factorySetting.getTvosVersion();
		MenuItemData factoryTvosNameItem  = new MenuItemData(TvosStr, mFactoryActivity.getResources()
				.getString(R.string.factory_tvos_version),TvosStr,
				EN_ItemType.EN_OPTIONTEXTVIEW);*/
		
//		String mName =mFactoryActivity.getResources()
//		.getString(R.string.factory_fp_version) +'\n'+mFactoryActivity.getResources()
//		.getString(R.string.factory_tvos_version);
//		MenuItemData factoryTvosAndFP =new MenuItemData(null, mName,fpStr+'\n'+TvosStr,
//				EN_ItemType.EN_OPTIONTEXTVIEW);
		
		
		/*2019-2-13 MDWStr =factorySetting.getMDWVersion();
		MenuItemData factoryMDWNameItem  = new MenuItemData(MDWStr, mFactoryActivity.getResources()
				.getString(R.string.factory_mdw_version),MDWStr,
				EN_ItemType.EN_OPTIONTEXTVIEW);*/
		
		DataVersionStr =factorySetting.getDataVersion();
		MenuItemData factoryDataVsersion  = new MenuItemData(DataVersionStr, mFactoryActivity.getResources()
				.getString(R.string.factory_data_version),DataVersionStr,
				MenuItemData.EN_ItemType.EN_OPTIONTEXTVIEW);
		
		/*2019-2-13 String mBootStr =factorySetting.getBootVersion();
		MenuItemData factoryMbootItem  = new MenuItemData(mBootStr, mFactoryActivity.getResources()
				.getString(R.string.factory_boot_version),mBootStr,
				EN_ItemType.EN_OPTIONTEXTVIEW);*/
		
		/*2019-3-11String m6M60Version =factorySetting.get6m40Version();
		if(m6M60Version==null||m6M60Version.length()==0){
			m6M60Version="N/A";
		}	
		MenuItemData factory6m60Version  = new MenuItemData("6m60", "6M60 Version",m6M60Version,
				EN_ItemType.EN_OPTIONTEXTVIEW);*/
		
		/*netActiveStr =factorySetting.getNetActive();
		MenuItemData netActiveItem  = new MenuItemData(netActiveStr, mFactoryActivity.getResources()
				.getString(R.string.factory_net_active_string),netActiveStr,
				EN_ItemType.EN_OPTIONTEXTVIEW);*/
		String pqIniVerStr=factorySetting.getPQIniVersion();
		MenuItemData factoryPqIniVsersion  = new MenuItemData(pqIniVerStr, "PQ Ini Version",pqIniVerStr,
				MenuItemData.EN_ItemType.EN_OPTIONTEXTVIEW);
		MenuItemData mKeypadTest = new MenuItemData("KeyPad Test");
		mKeypadTest.setOnEnterKey(new MenuItemData.onEnterRightKey() {

			public void onEnterRightKey() {
				// TODO Auto-generated method stub
				Intent mIntent = new Intent();
				mIntent.setClass(context, FactoryKeypadTestActivity.class);
				mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				context.startActivity(mIntent);
			}
		});
		
		String mTvToolStr = mFactoryActivity.getResources().getString(
				R.string.factory_pageone_tv_tools);
		MenuItemData mTvtoolsItem = new MenuItemData(mTvToolStr);
		mTvtoolsItem.setOnEnterKey(new MenuItemData.onEnterRightKey() {

			public void onEnterRightKey() {
				// TODO Auto-generated method stub
				Log.v("tv", "onEnterRightKey");
//				Intent mTvToolIntent = new Intent();
//				mTvToolIntent.setComponent(new ComponentName(
//						"com.changhong.tool6683",
//						"com.changhong.tool.TVtoolActivity"));
//				try {
//					mContext.startActivity(mTvToolIntent);
//					((Activity) FactoryActivity.mContext).finish();
//				} catch (ActivityNotFoundException e) {
//					Toast.makeText(mContext, "Please install TvTool !", Toast.LENGTH_LONG).show();
//				}
				Intent TVtoolServiceIntent = new Intent(mFactoryActivity,TvToolService.class);
				TVtoolServiceIntent.setAction("com.homwee.factory.TvToolService");
				mFactoryActivity.startService(TVtoolServiceIntent);
				mFactoryActivity.finish();
			}
		});
		
		MenuItemData mAutotest =new MenuItemData(0,"Auto test",0,1,0,mFactoryActivity.getResources().getStringArray(R.array.factoryCloseOrOpen),1, MenuItemData.EN_ItemType.EN_OPTIONVIEW);
		mAutotest.mInitValue =0;
		mAutotest.setOnValueChange(new MenuItemData.onValueChange() {
			
			public void valueChange(int value) {
				// TODO Auto-generated method stub
				Log.v("tv","setAutotest");
				if(value == 1){
					 Intent startIntent = new Intent("com.homwee.serialportinspection.action.START");
		             startIntent.setPackage("com.homwee.serialportinspection");
					 startIntent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND);
		             context.sendBroadcast(startIntent);
				}else{
					Intent startIntent = new Intent("com.homwee.serialportinspection.action.STOP");
	                startIntent.setPackage("com.homwee.serialportinspection");
					startIntent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND);
	                context.sendBroadcast(startIntent);
				}
				
			}
		});

		String tconBinVerStr =factorySetting.getTconBinVersion();
		MenuItemData tconBinVsersion  = new MenuItemData(tconBinVerStr,"TCON Bin Ver",tconBinVerStr,
				MenuItemData.EN_ItemType.EN_OPTIONTEXTVIEW);


		String projectSelectStr = mFactoryActivity.getResources().getString(R.string.factory_pageone_project_title);
		projectSelect=new MenuItemData(projectSelectStr);
		
		String projectIdStr = mFactoryActivity.getResources().getString(R.string.factory_pageone_project_id);
		String projectPanelNameStr = mFactoryActivity.getResources().getString(R.string.factory_pageone_project_panel);
		String projectCountryLanStr = mFactoryActivity.getResources().getString(R.string.factory_pageone_project_country);
		String projectLogoNameStr = mFactoryActivity.getResources().getString(R.string.factory_pageone_project_logoname);
		String projectMusicNameStr = mFactoryActivity.getResources().getString(R.string.factory_pageone_project_musicname);
		String projectAnimationNameStr = mFactoryActivity.getResources().getString(R.string.factory_pageone_project_Animationame);
		String projectKeyMapStr = mFactoryActivity.getResources().getString(R.string.factory_pageone_project_keymap);
		String projectInputSourceStr = mFactoryActivity.getResources().getString(R.string.factory_pageone_project_inputsource);

		selProjectid=factorySetting.getCurProjectIndex();
		projectID=new MenuItemData(selProjectid, projectIdStr ,selProjectid,MenuItemData.EN_ItemType.EN_OPTIONTEXTVIEW);
		projectID.setOnEnterKey(new MenuItemData.onEnterRightKey() {
			@Override
			public void onEnterRightKey() {
				ArrayList<String> pList=factorySetting.getProjectIdTotalList();

				final EditText et = new EditText(mContext);
				et.setInputType(InputType.TYPE_CLASS_NUMBER);
				//start
				LayoutInflater li =LayoutInflater.from(mContext);
				View quskrDetailsView = li.inflate(R.layout.bb,null);
				EditText mEtPid = quskrDetailsView.findViewById(R.id.et_pid);
				Button mBtnOk = quskrDetailsView.findViewById(R.id.btn_ok);
				Button mBtnCancel = quskrDetailsView.findViewById(R.id.btn_cancel);
				mBtnOk.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						String mPId = mEtPid.getText().toString();
						Toast.makeText(mContext,"This is OK Button, PID is "+mPId,Toast.LENGTH_LONG).show();
					}
				});



				//end

				AlertDialog alertDialog = new AlertDialog.Builder(mContext).setTitle("Select Project id")
						.setView(quskrDetailsView)
						.show();

				mBtnCancel.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						Toast.makeText(mContext,"This is Cancel Button",Toast.LENGTH_LONG).show();
						alertDialog.dismiss();
					}
				});
			}
		});


//		new AlertDialog.Builder(mContext).setTitle("Select Project id")
//				.setView(quskrDetailsView)
//				.setPositiveButton("OK", new DialogInterface.OnClickListener() {
//					@Override
//					public void onClick(DialogInterface dialogInterface, int i) {
//						String etPid=et.getText().toString();
//						int iPid=Integer.parseInt(etPid);
//						//String strMaxPid=MtkTvFApiInformation.getInstance().getIniParameter(
//						//		Contants.PATH_FACTORYINI, Contants.FACTORY_MAX_PROJECT_ID);
//
//						if(pList.size()!=0){
//							//int maxPid=Integer.parseInt(strMaxPid);
//							if(pList.contains(etPid)) {
//								MtkTvFApiInformation.getInstance().setIniParameter(
//										Contants.PATH_FACTORYINI, Contants.FACTORY_PROJECT_ID, etPid);
//								projectID.updateValue(iPid+"");
//								Log.d("CHFactory", "setProject id=" + etPid );
//							}else{
//								Toast.makeText(mContext, "Input error,please check pid",Toast.LENGTH_LONG).show();
//							}
//						}else{
//							Toast.makeText(mContext, "Can not found max Pid", Toast.LENGTH_LONG).show();
//						}
//
//					}
//				}).setNegativeButton("Cancel",null).show();
//	}
//});

		projectSaveToEep=new MenuItemData("Save ID to Eeprom","","Save ID to Eeprom",MenuItemData.EN_ItemType.EN_OPTIONTEXTVIEW);
		projectSaveToEep.setOnEnterKey(new MenuItemData.onEnterRightKey() {
			@Override
			public void onEnterRightKey() {
				factorySetting.WriteProjectIDToEeprom();
				MtkTvFApiSystem.getInstance().setEmmcEnvVar("db_table","0");
				//Toast.makeText(mContext, selProjectid.toString(),Toast.LENGTH_LONG).show();
			}
		});

		projectSave=new MenuItemData("Save ID","","Save ID",MenuItemData.EN_ItemType.EN_OPTIONTEXTVIEW);
		projectSave.setOnEnterKey(new MenuItemData.onEnterRightKey() {
			@Override
			public void onEnterRightKey() {
				String str=MtkTvFApiInformation.getInstance().getIniParameter(
						Contants.PATH_FACTORYINI, Contants.FACTORY_PROJECT_ID);
				factorySetting.setProjectId(str);
				MtkTvFApiSystem.getInstance().setEmmcEnvVar("db_table","0");
				Toast.makeText(mContext, "Sucess",Toast.LENGTH_LONG).show();
			}
		});

//		projectPanel=new MenuItemData("", projectPanelNameStr, "", MenuItemData.EN_ItemType.EN_OPTIONTEXTVIEW);
//		projectCountry=new MenuItemData("", projectCountryLanStr, "", MenuItemData.EN_ItemType.EN_OPTIONTEXTVIEW);
//		projectLogo=new MenuItemData("", projectLogoNameStr, "", MenuItemData.EN_ItemType.EN_OPTIONTEXTVIEW);
//		projectMusic=new MenuItemData("", projectMusicNameStr, "", MenuItemData.EN_ItemType.EN_OPTIONTEXTVIEW);
//		projectAnimation=new MenuItemData("", projectAnimationNameStr, "", MenuItemData.EN_ItemType.EN_OPTIONTEXTVIEW);
//		projectKeymap=new MenuItemData("", projectKeyMapStr, "", MenuItemData.EN_ItemType.EN_OPTIONTEXTVIEW);
//		projectInputSource = new MenuItemData("", projectInputSourceStr, "", MenuItemData.EN_ItemType.EN_OPTIONTEXTVIEW);

		mSubList.add(projectID);
		mSubList.add(projectSaveToEep);
		mSubList.add(projectSave);
		
//		mSubList.add(projectPanel);
//		mSubList.add(projectCountry);
//		mSubList.add(projectLogo);
//		mSubList.add(projectMusic);
//		mSubList.add(projectAnimation);
//		mSubList.add(projectKeymap);
//		mSubList.add(projectInputSource);
		
		projectID.mParentGroup =mListItemData;
		projectID.mParent = projectSelect;
		projectSelect.mSubChildGroup = mSubList;
		if (factorySetting.isFactoryShowNoFunc()) {
			mListItemData.add(panelSelect);
		}

		mListItemData.add(projectSelect);
		mListItemData.add(factoryPqNameItem);
		mListItemData.add(factoryAqNameItem);
		mListItemData.add(factoryFpNameItem);
		if(DataVersionStr !=null){
			mListItemData.add(factoryDataVsersion);
		}
		if(pqIniVerStr.length()>0 && pqIniVerStr !=null){
			mListItemData.add(factoryPqIniVsersion);
		}
		if(tconBinVerStr.length()>0 && tconBinVerStr !=null){
			mListItemData.add(tconBinVsersion);
		}
		mListItemData.add(mKeypadTest);

		//mListItemData.add(factory6m60Version);
		//add for start TV tools
		mListItemData.add(mTvtoolsItem);
		mListItemData.add(mAutotest);
		
		return mListItemData;
	}
	public String getSWVersion() {
		// TODO Auto-generated method stub
		return factorySetting.getSWVersion();
	}
	public String getSWDate() {
		// TODO Auto-generated method stub
		return factorySetting.getSWDate();
	}
	
}
